package com.dmgis.qxfw.dataservice.businessservice.yyhb.service.Impl;


import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.township_forecasttemp;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.airpol_reg_fcst;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.AirPolRegForecast;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.TownshipForecast;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.yyhb.service.InteractiveConsultationServiceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InteractiveConsultationServiceImpl implements InteractiveConsultationServiceI {

	ProcessRequestController procon=new ProcessRequestController();
	@Override
	public List<AirPolRegForecast> getAirInfo(String date) {
		List<airpol_reg_fcst> listData=new ArrayList<airpol_reg_fcst>();
		List<AirPolRegForecast> resultList=new ArrayList<AirPolRegForecast>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(cast(observtime as timestamp)+ cast((forecasttype)||'hour' as INTERVAL),'yyyy-MM-dd HH24:MI:SS') as observtime");
		columns.add("forecasttype");
		columns.add("cast(aqi as numeric(18,1)) as aqi");
		columns.add("cast(pm2p5 as numeric(18,1)) as pm2p5");
		columns.add("cast(pm10 as numeric(18,1)) as pm10");
		columns.add("cast(o3_1h as numeric(18,1)) as o3_1h");
		columns.add("cast(o3_8h as numeric(18,1)) as o3_8h");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[]{"observtime asc"});
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (date!=null) {
			strWhere.append(String.format(" and observtime='%s'",date));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "airpol_reg_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_reg_fcst>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("error!!!!");
		}
		if (listData!=null&&listData.size()>0) {
			for (airpol_reg_fcst airpol : listData) {
				AirPolRegForecast airpolregforecast=new AirPolRegForecast();
				Date date1 = airpol.getObservtime();
		        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		        airpolregforecast.setObservTime(sdf.format(date1));
				airpolregforecast.setAQI(airpol.getAqi());
				airpolregforecast.setForecastType(airpol.getForecasttype());
				airpolregforecast.setPM2p5(airpol.getPm2p5());
				airpolregforecast.setPM10(airpol.getPm10());
				airpolregforecast.setO3_1H(airpol.getO3_1h());
				airpolregforecast.setO3_8H(airpol.getO3_8h());
				resultList.add(airpolregforecast);
			}
		}
		return resultList;
	}
	@Override
	public List<TownshipForecast> getWeatherForecasts(String date, String timechar, int ntime1, int ntime2,
													  String stationId) {
		List<TownshipForecast> resultList = new ArrayList<TownshipForecast>();
		List<township_forecasttemp> result1 = new ArrayList<township_forecasttemp>();
		Map<String,String> selectParam = new HashMap<String,String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("StationName");
		columns.add("longitude");
		columns.add("latitude");
		columns.add("weather1");
		columns.add("winds");
		columns.add("windd");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		//条件where
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if(stationId!=null){
			strWhere.append(String.format("and stationid='%s'", stationId));
		}
		if(date!= null){
			strWhere.append(String.format(" and datechar='%s'", date));
		}
		if (timechar!=null) {
			strWhere.append(String.format(" and timechar='%s'", timechar));
		}
		if (ntime1!= 0) {
			strWhere.append(String.format(" and ntimes='%d'", ntime1));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "township_forecasttemp");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			result1 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecasttemp>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getStackTrace());
		}
		List<township_forecasttemp> result2 = new ArrayList<township_forecasttemp>();
		List<String> columns1 = new ArrayList<String>();
		columns1.add("stationid");
		columns1.add("StationName");
		columns1.add("weather1");
		columns1.add("winds");
		columns1.add("windd");
		columns1.add("maxtemp");
		columns1.add("minTemp");
		selectRequest.setColumns(columns1.toArray(new String[columns1.size()]));
		//条件where
		StringBuilder strWhere1 = new StringBuilder();
		int whereCount1 = 0;
		if(stationId!=null){
			strWhere1.append(String.format("and stationid='%s'", stationId));
		}
		if(date!= null){
			strWhere1.append(String.format(" and datechar='%s'", date));
		}
		if (timechar!=null) {
			strWhere1.append(String.format(" and timechar='%s'", timechar));
		}
		if (ntime2!= 0) {
			strWhere1.append(String.format(" and ntimes='%d'", ntime2));
		}
		if (whereCount1 == 0 && !StringUtil.IsNullOrEmpty(strWhere1.toString())) {
			strWhere1 = strWhere1.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere1.toString());
		try {
			selectParam.put("method", "township_forecasttemp");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			result2 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecasttemp>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getStackTrace());
		}
		if(result1!=null &&result1.size()>0 && result2!=null &&result2.size()>0){
			TownshipForecast result = new TownshipForecast();
			for(township_forecasttemp result11:result1){
				result.setStationID(result11.getStationid());
				result.setStationName(result11.getStationname());
				result.setLongitude(result11.getLongitude().floatValue());
				result.setLatitude(result11.getLatitude().floatValue());
				result.setWeather1(result11.getWeather1());
				result.setWindD(result11.getWindd().floatValue());
				result.setWindS(result11.getWinds().floatValue());
			}
			for(township_forecasttemp result22:result2){
				result.setWeather2(result22.getWeather1());
				result.setWindD2(result22.getWindd().floatValue());
				result.setWindS2(result22.getWinds().floatValue());
				result.setMinTemp(result22.getMintemp().floatValue());
				result.setMaxTemp(result22.getMaxtemp().floatValue());
			}
			resultList.add(result);
		}
		return resultList;
	}
	@Override
	public Date productInitTime() {
		Date date=new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime"};
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "observtime desc"});
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "airpol_reg_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = procon.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_reg_fcst>>() {
			}).get(0).getObservtime();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	

}
